#!/bin/bash
#通过jenkins,一键添加cdn
var_name=$1
service_ip=$2
service_port=$3
service_name=$4
source_ip=$5

cdn_name=$var_name.cyberwisdom.net.cn
base_dir=/data/jenkins/workspace/devops
jenkins_shell_dir=$base_dir/jenkins/shell
notice_file=$jenkins_shell_dir/notice_jenkins.py
huawei_cdnlog=$base_dir/tmp/huawei_cdninfo.log


#创建华为cdn
create_huawei_cdn(){
    mkdir -p $base_dir/tmp
    hcloud CDN CreateDomain/v1 --cli-region="cn-north-1" --domain.domain_name="$cdn_name" --domain.sources.1.active_standby=1 --domain.sources.1.ip_or_domain="$source_ip" --domain.sources.1.origin_type="ipaddr" --domain.business_type="video" --domain.service_area="mainland_china"  >$huawei_cdnlog
}

#创建dns解析
create_ali_dns(){
    aliyun configure set --profile akProfile --mode AK --region cn-hangzhou --access-key-id xx --access-key-secret xx
    id_alilog=$base_dir/tmp/ali_record_id.log
    dns_rr=`cat $huawei_cdnlog |  grep -w cname | awk -F '[:|,]' '{print $2}' |   sed 's/\"//g'| sed 's/^[ ]*//g'`
    #添加cname
    if  [[ $source_ip == xxip ]];then
        aliyun alidns AddDomainRecord --region cn-hangzhou --Type CNAME  --DomainName 'cyberwisdom.net.cn'   --RR "$var_name"  --Value "$dns_rr" >$id_alilog
    else
        aliyun alidns AddDomainRecord --region cn-hangzhou --Type A  --DomainName 'cyberwisdom.net.cn'   --RR "$var_name"  --Value "$$source_ip" >$id_alilog  
    fi
    #获取记录值
    record_id=`cat $id_alilog | grep RecordId  |awk -F ':|"' '{print $5}'`
    #添加备注
    aliyun alidns UpdateDomainRecordRemark --region cn-hangzhou --RecordId $record_id  --Remark "$service_name"
}


add_nginxconf(){
if [[ $source_ip  == "xxip" ]];then
    nginx_ip=192.168.2.200
    gossh="ssh -i /app/pem/nick.pem"
    get_http="192.168.4.200:10000"
else
    nginx_ip=192.168.3.200
    gossh="ssh -i /app/pem/nick.pem  -J root@xx:xx"
    get_http="ip:port"
fi

#远程执行命令
cat > /data/openresty/tmp/cdn_variable.log <<- EOF
$cdn_name
$service_ip
$service_port
$service_name
EOF

$gossh $nginx_ip "wget http://$get_http/tmp/cdn_variable.log -O /tmp/cdn_variable.log"

$gossh $nginx_ip '''
cdn_log=/tmp/cdn_variable.log
cdn_name=`head -n 1  $cdn_log`
service_ip=`head -n 2 $cdn_log |tail -n 1`
service_port=`head -n 3 $cdn_log |tail -n 1`
service_name=`head -n 4 $cdn_log |tail -n 1`

cat >> /etc/nginx/conf.d/test.conf <<- EOF

#$service_name
server {
    listen       80;
    server_name  $cdn_name;
    location / {
        proxy_pass http://$service_ip:$service_port;
    }
}
EOF
/usr/sbin/nginx -s reload
'''  
}

#设置相关规则
set_huawei_cdn(){
    domain_id=`cat $huawei_cdnlog  |  grep -w  domain_id | awk -F '[:|,]' '{print $2}' |   sed 's/\"//g'  | sed 's/^[ ]*//g'| uniq`
    #设置tag
    cdn_key=test
    hcloud CDN CreateTags/v1 --cli-region="cn-north-1" --resource_id="$domain_id"  --tags.1.key="$cdn_key"  --tags.1.value="$service_name" 
    #配置缓存规则
    hcloud CDN UpdateCacheRules/v1 --cli-region="cn-north-1" --domain_id="$domain_id" --cache_config.rules.1.rule_type=1 --cache_config.rules.1.ttl_type=4 --cache_config.rules.1.priority=5 --cache_config.rules.1.ttl=180 --cache_config.rules.1.content=".mp3;.mp4;.ts;.MP4;.MP3;.ts;.m3u8"  --cache_config.rules.2.rule_type=0 --cache_config.rules.2.ttl_type=1 --cache_config.rules.2.priority=1 --cache_config.rules.2.ttl=0 
    #修改源站端口
    if [[ $source_ip  == "xxx" ]];then
        hcloud CDN UpdateDomainOrigin/v1 --cli-region="cn-north-1" --domain_id="$domain_id" --origin.sources.1.active_standby=1 --origin.sources.1.ip_or_domain="$source_ip" --origin.sources.1.origin_type="ipaddr" --origin.sources.1.https_port=442 --origin.sources.1.http_port=88
    fi
    #添加证书
    hcloud CDN UpdateHttpsInfo/v1 --cli-region="cn-north-1" --domain_id="$domain_id" --https.cert_name="cyberwisdom" --https.certificate="xxx" --https.certificate_type=1 --https.https_status=1 --https.force_redirect_config.redirect_type="https" --https.force_redirect_config.switch=1
}


CURRENT_DIR=$(cd $(dirname $0); pwd)
cd  $CURRENT_DIR
#2网段才创建cdn,3网段只创建域名解析
if  [[ $source_ip == xxx ]];then
    create_huawei_cdn
    create_ali_dns
    add_nginxconf
    sleep 60
    set_huawei_cdn
else
    create_ali_dns
    add_nginxconf
fi

python3 $notice_file  $service_name   $cdn_name  测试  成功 $select_option